package com.googlecode.gmf.data;

import java.util.LinkedList
import java.util.Hashtable

class Web {
	
	Set<Node> nodes = new HashSet<Node>()
	
	Map<Node, Node> links = new Hashtable<Node, Node>()
	
	public int size() {
		return nodes.size()
	}
	
	public static Web getAnEmptyWeb() {
		Web emptyWeb = new Web()
		emptyWeb.emptyWeb()
		return emptyWeb
	}
	
	public Web emptyWeb() {
		nodes.clear()
	}
	
	public void addNode(aNode) {
		synchronized (nodes) {
			nodes.add(aNode)	
		}
	}

	public Node findByTitle(aTitle) {
		return nodes.find { it.title == aTitle }
	}
	
	public link(Node firstNode, Node secondNode) {
		links.put(firstNode, secondNode)
	}
	
	public List<Node> linksFrom(Node aNode) {
		links.findAll { entry -> entry.key.equals(aNode) }.collect { entry -> entry.value }
	}
	
	Web() {
		def initialNode = new Node(
				title: 'Welcome to your new MindField',
				content: 'Enter your content here')
		nodes.add(initialNode)
	}
}